package com.easyjava.qingke.memo.command.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Pattern;

import com.easyjava.qingke.memo.command.SystemCommand;
import com.easyjava.qingke.memo.mybatis.pojo.Days;
import com.easyjava.qingke.memo.mybatis.pojo.MemoContent;
import com.easyjava.qingke.memo.util.MemoConsole;

public class SelectCommand extends SystemCommand {

	public void excute() {

		MemoConsole.println("1.按时间查询并降序排列备忘录内容");
		MemoConsole.println("2.按单个自定义标签查询备忘录内容，并按时间降序排列");
		MemoConsole.println("3.多个自定义标签“并”查询备忘录内容，并按时间降序排列");
		MemoConsole.println("4.按标题或内容关键字查询备忘录内容，并按时间降序排列");
	

		int id = MemoConsole.promptInt("请输入想要操作的数字：");
		while (id > 0) {
			if (1==id) {
				try {
					selectMemoContentByTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}
				break;
			} else if (2==id) {
				selectMemoCotentByOneLabel();
				break;
			} else if (3==id) {
				selectMemoContentByMoreLabel();
				break;
			} else if (4==id) {
                selectMemoContentByTitleOrContent();
				break;
			} else {
				MemoConsole.println("该数字不存在！");
				id = MemoConsole.promptInt("请重新输入想要操作的数字：");
			}

		}
	}

	private void selectMemoContentByTitleOrContent() {
		   dao.selectMemoContent();
		   MemoConsole.println();
		   
		   MemoContent memoContent =new MemoContent();
		   String type =MemoConsole.prompt("标题/关键字：");
		   
		   while(type!=null){
		   if("标题".equals(type)){
			   String title ="%"+MemoConsole.prompt("请输入标题：")+"%";
			   while(!dao.isTitleExists(title)){
				   MemoConsole.println("该标题不存在！");
				   title=MemoConsole.prompt("请重新输入标题：");
			   }
			   memoContent.setTitle(title);
			   dao.selectMemoContentByTitleOrContent(memoContent);
			   break;
		   }else if("关键字".equals(type)){
			   String content="%"+MemoConsole.prompt("请输入关键字：")+"%";
			   while(!dao.isContentExists(content)){
				   MemoConsole.println("无法匹配！");
				   content=MemoConsole.prompt("请重新输入关键字：");
			   }
			   memoContent.setContent(content);
			   dao.selectMemoContentByTitleOrContent(memoContent);
			   break;
		   }else
			   type=MemoConsole.prompt("标题/关键字：");
		   }
		   
		   
	}

	private void selectMemoContentByMoreLabel() {
             MemoConsole.println("暂未实现！");		
	}

	private void selectMemoCotentByOneLabel() {
		dao.selectLabel();
		
		MemoConsole.println();
		
		String name=MemoConsole.prompt("请输入所要查询的标签名称：");
		while(!dao.isLabelNameExists(name)){
			MemoConsole.println("该名称不存在！");
			name=MemoConsole.prompt("请重新输入所要查询的标签名称：");
		}
		dao.selectMemoContentByLabelName(name);
		
	}

	private void selectMemoContentByTime() throws ParseException {
		
		String time1=MemoConsole.prompt("请输入第一个时间：（格式：YYYY-mm-dd）");
		while(!matcher(time1)){
			MemoConsole.println("格式错误！");
			time1=MemoConsole.prompt("请重新输入第一个时间：（格式：YYYY-mm-dd）");
		}
		String time2=MemoConsole.prompt("请输入第二个时间：（格式：YYYY-mm-dd）");
		while(!matcher(time2)){
			MemoConsole.println("格式错误！");
			time2=MemoConsole.prompt("请重新输入第二个时间：（格式：YYYY-mm-dd）");
		}
		Date start=toDate(time1);
		Date end=toDate(time2);
		if(start.after(end)){
			MemoConsole.println("err input");
			MemoConsole.println("start date can not after end date.");
			return;
		}
		Days days=new Days();
		days.setStartTime(start);
		days.setEndTime(new Date(end.getTime()+24*60*60*1000));
		
        dao.selectMemoContentByTime(days);		
	}
	
	private boolean matcher(String str){
		Pattern datePattern = Pattern.compile("[0-9]{4}[^0-9][0-9]{1,2}[^0-9][0-9]{1,2}");
		if(!datePattern.matcher(str).find())
			return false;
		String[] ymd=str.split("[^0-9]");
		int year=Integer.parseInt(ymd[0]);
		if(year<1900)
			return false;
		int month=Integer.parseInt(ymd[1]);
		if(month>12)
			return false;
		int day=Integer.parseInt(ymd[2]);
		if(day>31)
			return false;
		try{
			new Date(year,month,day);
		}catch(Exception e){
			return false;
		}
		
		return true;
	}
	
	public Date toDate(String str){
		String[] ymd=str.split("[^0-9]");
		int year=Integer.parseInt(ymd[0])-1900;
		int month=Integer.parseInt(ymd[1])-1;
		int day=Integer.parseInt(ymd[2]);
		return new Date(year,month,day);
	}

}
